package com.ssm.controller;

import com.ssm.domain.Permission;
import com.ssm.domain.Role;
import com.ssm.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
/**
 * @Author: my_code
 * @Description: 权限资源管理
 * @Date: Create in 17:21 2020/8/26
 */

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/findAll.do")
    public ModelAndView findAll(){
        ModelAndView mv = new ModelAndView();
        List<Permission> permissionList = permissionService.findAll();
        mv.addObject("permissionList", permissionList);
        mv.setViewName("permission-list");
        return mv;
    }

    //此方法逻辑过程与RoleController中findUserByRoleId方法类似
    @RequestMapping("/findRoleByPermissionId.do")
    public ModelAndView findRoleByPermissionId(@RequestParam(name = "id")String permissionId) throws Exception {
        ModelAndView mv = new ModelAndView();
        Permission permission = permissionService.findById(permissionId);
        List<Role> otherRoles = permissionService.findOtherRoleById(permissionId);
        mv.addObject("permission",permission);
        mv.addObject("otherRoles",otherRoles);
        mv.setViewName("permission-role-add");
        return mv;
    }

    @RequestMapping("/addRoleToPermission.do")
    public String addRoleToPermission(String permissionId, @RequestParam(name = "ids")String[] roleIds){
        permissionService.save(permissionId, roleIds);
        return "redirect:findRoleByPermissionId.do?id="+permissionId;
    }
}
